package com.hqd.jjvm.jnative.io;

import com.hqd.jjvm.jnative.JNativeRegistry;
import com.hqd.jjvm.rtda.JThread;
import com.hqd.jjvm.rtda.LocalVars;
import com.hqd.jjvm.rtda.obj.JObject;

public class JFileOutputStreamNativeRegistry extends JNativeRegistry {
    private static final JFileOutputStreamNativeRegistry instance = new JFileOutputStreamNativeRegistry();
    protected static final String CLASS_PATH = "java/io/FileOutputStream";

    static {
        registry(CLASS_PATH, "initIDs", "()V", JFileOutputStreamNativeRegistry::registerNatives);
        registry(CLASS_PATH, "writeBytes", "([BIIZ)V", JFileOutputStreamNativeRegistry::writeBytes);
    }

    private static void writeBytes(JThread jThread) {
        LocalVars vars = jThread.getJvmStack().getTop().getLocalVars();
        JObject bytesRef = vars.getRefVal(1);
        int off = vars.getIntVal(2);
        int len = vars.getIntVal(3);
        byte[] bytes = (byte[]) bytesRef.getData();
        System.out.print(new String(bytes, off, len));
    }

    public static JFileOutputStreamNativeRegistry getInstance() {
        return instance;
    }
}
